// Clang modern C++20 features demo
#include <iostream>
#include <string>
#include <vector>
#include <concepts>
#include <ranges>
#include <algorithm>
#include <numeric>

// C++20 concepts example
template<typename T>
concept Numeric = std::integral<T> || std::floating_point<T>;

// C++20 constrained template
template<Numeric T>
T advanced_max(T a, T b) {
    return (a > b) ? a : b;
}

namespace ClangDemo {
    // Modern C++20 features demonstration
    class ModernCalculator {
    private:
        std::string name;
        
    public:
        ModernCalculator(const std::string& calc_name) : name(calc_name) {}
        
        // Auto return type deduction
        auto calculate_sum(const std::vector<int>& numbers) {
            // Using standard accumulate
            auto sum = std::accumulate(numbers.begin(), numbers.end(), 0);
            std::cout << name << " calculates sum: " << sum << std::endl;
            return sum;
        }
        
        // C++20 concept constraints
        template<Numeric T>
        auto find_max(const std::vector<T>& values) {
            if (values.empty()) return T{};
            auto max_val = *std::ranges::max_element(values);
            std::cout << name << " finds max: " << max_val << std::endl;
            return max_val;
        }
    };
    
    // C++20 structured bindings and ranges
    void demonstrate_ranges() {
        std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        
        std::cout << "C++20 Ranges demo:" << std::endl;
        
        // Filter even numbers and print
        auto even_numbers = numbers | std::views::filter([](int n) { return n % 2 == 0; });
        std::cout << "Even numbers: ";
        for (auto n : even_numbers) {
            std::cout << n << " ";
        }
        std::cout << std::endl;
        
        // Transform and filter
        auto doubled_odds = numbers 
            | std::views::filter([](int n) { return n % 2 == 1; })
            | std::views::transform([](int n) { return n * 2; });
            
        std::cout << "Doubled odd numbers: ";
        for (auto n : doubled_odds) {
            std::cout << n << " ";
        }
        std::cout << std::endl;
    }
}

int main() {
    std::cout << "=== Clang C++20 Modern Features Demo ===" << std::endl;
    
    ClangDemo::ModernCalculator calc("Clang Calculator");
    
    // Test numeric calculations
    std::vector<int> numbers = {42, 17, 83, 29, 56};
    calc.calculate_sum(numbers);
    calc.find_max(numbers);
    
    // Test concept constraints
    std::cout << "\nConcept constraints test:" << std::endl;
    auto max_int = advanced_max(100, 200);
    auto max_double = advanced_max(3.14, 2.71);
    std::cout << "max(100, 200) = " << max_int << std::endl;
    std::cout << "max(3.14, 2.71) = " << max_double << std::endl;
    
    // Ranges demonstration
    std::cout << std::endl;
    ClangDemo::demonstrate_ranges();
    
    std::cout << "\n=== Clang Modern Features Demo Complete ===" << std::endl;
    return 0;
}
